package com.ruoyi.act.module.api;

import com.ruoyi.act.module.domain.ActHistory;
import com.ruoyi.act.module.service.IHistoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.activiti.engine.history.HistoricTaskInstance;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 工作流租户对象 act_biz_info
 *
 * @author hcj
 * @date 2020-12-24
 */
@RestController
@RequestMapping("/api/history")
public class ApiHistoryController extends BaseController {

    @Autowired
    private IHistoryService iHistoryService;

    private final static String suffix = ".png";

    /**
     * 查询任务执行记录
     *
     * @param actHistory
     * @return
     */
    @PostMapping("/task/list")
    public AjaxResult taskList(@RequestBody ActHistory actHistory) {
        List<HistoricTaskInstance> list = iHistoryService.findTaskList(actHistory);
        List<Map<String, Object>> mapList = new ArrayList<>();
        list.forEach(info -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", info.getId());
            map.put("taskName", info.getName());
            map.put("assignee", info.getAssignee() == null ? "" : info.getAssignee());
            map.put("startTime", info.getStartTime());
            map.put("endTime", info.getEndTime() == null ? "" : info.getEndTime());
            map.put("businessKey", info.getBusinessKey());
            mapList.add(map);
        });
        return AjaxResult.success(mapList);
    }

    /**
     * 查询任务执行记录
     *
     * @param actHistory
     * @return
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ActHistory actHistory) {
        List<Map<String, Object>> list = iHistoryService.findActivityList(actHistory);
        return AjaxResult.success(list);
    }

    /**
     * 下载流程图片
     *
     * @param actHistory
     * @param response
     * @throws IOException
     */
    @PostMapping("/generate")
    public void generateDiagram(@RequestBody ActHistory actHistory, HttpServletResponse response) throws IOException, TranscoderException {

        InputStream inputStream = iHistoryService.generateDiagram(actHistory);

        if (inputStream == null) {
            return;
        }

        String fileName = new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date()) + suffix;
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        IOUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();
    }

}
